import axios from "axios";
import useUserStore from "@/store/modules/user";
import nprogress from 'nprogress';
import "nprogress/nprogress.css";

let request = axios.create({
    baseURL:'/api', //请求地址的设置
    timeout:5000 //超时时间设置
});

// 请求拦截器
request.interceptors.request.use((config) => {
    let userStore =  useUserStore();
    if (userStore.userInfo) {
        config.headers.token = userStore.userInfo;
    }
    nprogress.start();
    return config;
});

// 响应拦截器
request.interceptors.response.use((response) => {
    nprogress.done();
    return response.data;
},error => {
    return Promise.reject(new Error(error.message))
});


export default request;